﻿using CPPEI.Coolzon.UI.Views;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace CPPEI.Coolzon.Designer.Views
{
    public partial class DesignView
    {
        public bool IsResize { get; set; }
        public int ResizeDirection { get; set; }
        public Point ResizeBeginPointInCanvas { get; set; }
        public Point ResizeBeginPartPos { get; set; }
        public Size ResizeBeginPartSize { get; set; }

        /// <summary>
        /// 在调整大小位置时鼠标点击的偏移值， 目前只有在移动位置时这个值才有意义
        /// </summary>
        public Point ResizeOffset { get; set; }

        void OnDesignBorder_SizePointPress(int posIndex, Point pointInCanvas, Point offset)
        {
            Debug.WriteLine("OnDesignBorder_SizePointPress");
            //_designData.UpdateState(DesignState.ResizePart);

            IsResize = true;
            ResizeDirection = posIndex;
            ResizeOffset = offset;

            this.ResizeBeginPointInCanvas = pointInCanvas;
            this.ResizeBeginPartPos = new Point(this._selectedPart.XLeft * this._ctlMainFrame.ZoomFactor,
                this._selectedPart.XTop * this._ctlMainFrame.ZoomFactor);
            this.ResizeBeginPartSize = new Size(this._selectedPart.XWidth * this._ctlMainFrame.ZoomFactor,
                this._selectedPart.XHeight * this._ctlMainFrame.ZoomFactor);
        }

        /// <summary>
        /// alt键按下时的移动模式
        /// </summary>
        /// <param name="resizeDirection"></param>
        /// <param name="curPointInCanvas"></param>
        /// <param name="offset"></param>
        public void ResizePart4ALT(int resizeDirection, Point curPointInCanvas, Point offset)
        {
            double deltaWidth = this.ResizeBeginPointInCanvas.X - curPointInCanvas.X;
            double deltaHeight = this.ResizeBeginPointInCanvas.Y - curPointInCanvas.Y;

            deltaWidth = deltaWidth * this._ctlMainFrame.ZoomFactor;
            deltaHeight = deltaHeight * this._ctlMainFrame.ZoomFactor;

            if (resizeDirection == 0)
            {
                #region // 左上
                double width = this.ResizeBeginPartSize.Width + deltaWidth;
                if (width < 0)
                {
                    width = 0;
                }
                double height = this.ResizeBeginPartSize.Height + deltaHeight;
                if (height < 0)
                {
                    height = 0;
                }

                double left = this.ResizeBeginPartPos.X - deltaWidth;
                double top = this.ResizeBeginPartPos.Y - deltaHeight;

                this._selectedPart.XLeft = left;
                this._selectedPart.XTop = top;

                width = ((int)(width / ALT_MOVE_DISTANCE)) * ALT_MOVE_DISTANCE;
                height = ((int)(height / ALT_MOVE_DISTANCE)) * ALT_MOVE_DISTANCE;
                this._selectedPart.XWidth = width;
                this._selectedPart.XHeight = height;
                #endregion
            }
            else if (resizeDirection == 1)
            {
                #region // 上
                double height = this.ResizeBeginPartSize.Height + deltaHeight;
                if (height < 0)
                {
                    height = 0;
                }

                double top = this.ResizeBeginPartPos.Y - deltaHeight;

                this._selectedPart.XTop = top;
                height = ((int)(height / ALT_MOVE_DISTANCE)) * ALT_MOVE_DISTANCE;
                this._selectedPart.XHeight = height;
                #endregion
            }
            else if (resizeDirection == 2)
            {
                #region 右上
                double height = this.ResizeBeginPartSize.Height + deltaHeight;
                if (height < 0)
                {
                    height = 0;
                }
                double width = this.ResizeBeginPartSize.Width - deltaWidth;
                if (width < 0)
                {
                    width = 0;
                }

                double top = this.ResizeBeginPartPos.Y - deltaHeight;

                this._selectedPart.XTop = top;

                width = ((int)(width / ALT_MOVE_DISTANCE)) * ALT_MOVE_DISTANCE;
                this._selectedPart.XWidth = width;
                height = ((int)(height / ALT_MOVE_DISTANCE)) * ALT_MOVE_DISTANCE;
                this._selectedPart.XHeight = height;
                #endregion
            }
            else if (resizeDirection == 3)
            {
                #region // 左
                double width = this.ResizeBeginPartSize.Width + deltaWidth;
                if (width < 0)
                {
                    width = 0;
                }

                double left = this.ResizeBeginPartPos.X - deltaWidth;

                this._selectedPart.XLeft = left;
                width = ((int)(width / ALT_MOVE_DISTANCE)) * ALT_MOVE_DISTANCE;
                this._selectedPart.XWidth = width;
                #endregion
            }
            else if (resizeDirection == 4)
            {
                // 移动组件的位置
                double left = this.ResizeBeginPartPos.X - deltaWidth;
                double top = this.ResizeBeginPartPos.Y - deltaHeight;

                this._selectedPart.XLeft = left;
                this._selectedPart.XTop = top;
            }
            else if (resizeDirection == 5)
            {
                #region 右
                double width = this.ResizeBeginPartSize.Width - deltaWidth;
                if (width < 0)
                {
                    width = 0;
                }

                width = ((int)(width / ALT_MOVE_DISTANCE)) * ALT_MOVE_DISTANCE;
                this._selectedPart.XWidth = width;
                #endregion
            }
            else if (resizeDirection == 6)
            {
                #region 左下
                double width = this.ResizeBeginPartSize.Width + deltaWidth;
                if (width < 0)
                {
                    width = 0;
                }

                double height = this.ResizeBeginPartSize.Height - deltaHeight;
                if (height < 0)
                {
                    height = 0;
                }

                double left = this.ResizeBeginPartPos.X - deltaWidth;

                this._selectedPart.XLeft = left;

                width = ((int)(width / ALT_MOVE_DISTANCE)) * ALT_MOVE_DISTANCE;
                this._selectedPart.XWidth = width;
                height = ((int)(height / ALT_MOVE_DISTANCE)) * ALT_MOVE_DISTANCE;
                this._selectedPart.XHeight = height;
                #endregion
            }
            else if (resizeDirection == 7)
            {
                // 下
                double height = this.ResizeBeginPartSize.Height - deltaHeight;
                if (height < 0)
                {
                    height = 0;
                }

                height = ((int)(height / ALT_MOVE_DISTANCE)) * ALT_MOVE_DISTANCE;
                this._selectedPart.XHeight = height;
            }
            else if (resizeDirection == 8)
            {
                double width = this.ResizeBeginPartSize.Width - deltaWidth;
                if (width < 0)
                {
                    width = 0;
                }

                double height = this.ResizeBeginPartSize.Height - deltaHeight;
                if (height < 0)
                {
                    height = 0;
                }

                width = ((int)(width / ALT_MOVE_DISTANCE)) * ALT_MOVE_DISTANCE;
                this._selectedPart.XWidth = width;
                height = ((int)(height / ALT_MOVE_DISTANCE)) * ALT_MOVE_DISTANCE;
                this._selectedPart.XHeight = height;
            }

            Size borderSize = new Size(this._selectedPart.XWidth, this._selectedPart.XHeight);
            this._ctlDesignBorder.SetSize(borderSize);

            Point borderOffset = _selectedPart.TranslatePoint(new Point(0, 0), this._ctlCanvas);
            _ctlDesignBorder.SetOffset(borderOffset);

            this.ShowPartSizeLabel(curPointInCanvas,
                this._ctlCanvas,
                this._selectedPart.XWidth,
                this._selectedPart.XHeight);
        }

        /// <summary>
        /// ctrl键按下时的移动模式
        /// </summary>
        /// <param name="resizeDirection"></param>
        /// <param name="curPointInCanvas"></param>
        /// <param name="offset"></param>
        public void ResizePart4CTRL(int resizeDirection, Point curPointInCanvas, Point offset)
        {
            double deltaWidth = this.ResizeBeginPointInCanvas.X - curPointInCanvas.X;
            double deltaHeight = this.ResizeBeginPointInCanvas.Y - curPointInCanvas.Y;

            if (resizeDirection == 0)
            {
                #region // 左上
                double width = this.ResizeBeginPartSize.Width + deltaWidth;
                if (width < 0)
                {
                    width = 0;
                }
                double height = this.ResizeBeginPartSize.Height + deltaHeight;
                if (height < 0)
                {
                    height = 0;
                }

                double left = this.ResizeBeginPartPos.X - deltaWidth;
                double top = this.ResizeBeginPartPos.Y - deltaHeight;

                this._selectedPart.XLeft = left;
                this._selectedPart.XTop = top;

                width = ((int)(width / CTRL_MOVE_DISTANCE)) * CTRL_MOVE_DISTANCE;
                height = ((int)(height / CTRL_MOVE_DISTANCE)) * CTRL_MOVE_DISTANCE;
                this._selectedPart.XWidth = width;
                this._selectedPart.XHeight = height;
                #endregion
            }
            else if (resizeDirection == 1)
            {
                #region // 上
                double height = this.ResizeBeginPartSize.Height + deltaHeight;
                if (height < 0)
                {
                    height = 0;
                }

                double top = this.ResizeBeginPartPos.Y - deltaHeight;

                this._selectedPart.XTop = top;
                height = ((int)(height / CTRL_MOVE_DISTANCE)) * CTRL_MOVE_DISTANCE;
                this._selectedPart.XHeight = height;
                #endregion
            }
            else if (resizeDirection == 2)
            {
                #region 右上
                double height = this.ResizeBeginPartSize.Height + deltaHeight;
                if (height < 0)
                {
                    height = 0;
                }
                double width = this.ResizeBeginPartSize.Width - deltaWidth;
                if (width < 0)
                {
                    width = 0;
                }

                double top = this.ResizeBeginPartPos.Y - deltaHeight;

                this._selectedPart.XTop = top;

                width = ((int)(width / CTRL_MOVE_DISTANCE)) * CTRL_MOVE_DISTANCE;
                this._selectedPart.XWidth = width;
                height = ((int)(height / CTRL_MOVE_DISTANCE)) * CTRL_MOVE_DISTANCE;
                this._selectedPart.XHeight = height;
                #endregion
            }
            else if (resizeDirection == 3)
            {
                #region // 左
                double width = this.ResizeBeginPartSize.Width + deltaWidth;
                if (width < 0)
                {
                    width = 0;
                }

                double left = this.ResizeBeginPartPos.X - deltaWidth;

                this._selectedPart.XLeft = left;
                width = ((int)(width / CTRL_MOVE_DISTANCE)) * CTRL_MOVE_DISTANCE;
                this._selectedPart.XWidth = width;
                #endregion
            }
            else if (resizeDirection == 4)
            {
                // 移动组件的位置
                double left = this.ResizeBeginPartPos.X - deltaWidth;
                double top = this.ResizeBeginPartPos.Y - deltaHeight;

                this._selectedPart.XLeft = left;
                this._selectedPart.XTop = top;
            }
            else if (resizeDirection == 5)
            {
                #region 右

                deltaWidth = deltaWidth * this._ctlMainFrame.ZoomFactor;
                double width = this.ResizeBeginPartSize.Width - deltaWidth;
                if (width < 0)
                {
                    width = 0;
                }

                width = ((int)(width / CTRL_MOVE_DISTANCE)) * CTRL_MOVE_DISTANCE; 
                this._selectedPart.XWidth = width;
                #endregion
            }
            else if (resizeDirection == 6)
            {
                #region 左下
                double width = this.ResizeBeginPartSize.Width + deltaWidth;
                if (width < 0)
                {
                    width = 0;
                }

                double height = this.ResizeBeginPartSize.Height - deltaHeight;
                if (height < 0)
                {
                    height = 0;
                }

                double left = this.ResizeBeginPartPos.X - deltaWidth;

                this._selectedPart.XLeft = left;

                width = ((int)(width / CTRL_MOVE_DISTANCE)) * CTRL_MOVE_DISTANCE;
                this._selectedPart.XWidth = width;
                height = ((int)(height / CTRL_MOVE_DISTANCE)) * CTRL_MOVE_DISTANCE;
                this._selectedPart.XHeight = height;
                #endregion
            }
            else if (resizeDirection == 7)
            {
                // 下
                double height = this.ResizeBeginPartSize.Height - deltaHeight;
                if (height < 0)
                {
                    height = 0;
                }

                height = ((int)(height / CTRL_MOVE_DISTANCE)) * CTRL_MOVE_DISTANCE;
                this._selectedPart.XHeight = height;
            }
            else if (resizeDirection == 8)
            {
                double width = this.ResizeBeginPartSize.Width - deltaWidth;
                if (width < 0)
                {
                    width = 0;
                }

                double height = this.ResizeBeginPartSize.Height - deltaHeight;
                if (height < 0)
                {
                    height = 0;
                }

                width = ((int)(width / CTRL_MOVE_DISTANCE)) * CTRL_MOVE_DISTANCE;
                this._selectedPart.XWidth = width;
                height = ((int)(height / CTRL_MOVE_DISTANCE)) * CTRL_MOVE_DISTANCE;
                this._selectedPart.XHeight = height;
            }

            Size borderSize = new Size(this._selectedPart.XWidth, this._selectedPart.XHeight);
            this._ctlDesignBorder.SetSize(borderSize);

            Point borderOffset = _selectedPart.TranslatePoint(new Point(0, 0), this._ctlCanvas);
            _ctlDesignBorder.SetOffset(borderOffset);

            this.ShowPartSizeLabel(curPointInCanvas, 
                this._ctlCanvas, 
                this._selectedPart.XWidth,
                this._selectedPart.XHeight);
        }

        /// <summary>
        /// 显示设计边框
        /// </summary>
        /// <param name="selectedPartInstance"></param>
        /// <param name="canvas"></param>
        private void ShowDesignBorder(Coolzon.UI.Part selectedPartInstance, Canvas canvas)
        {
            if (selectedPartInstance == null)
            {
                return;
            }

            Coolzon.UI.Part parentPart = selectedPartInstance.ParentPart as Coolzon.UI.Part;

            if (parentPart != null)
            {
                Point offsetOfPart = (parentPart as Coolzon.UI.IPartContainer).GetContentContainer().TranslatePoint(new Point(selectedPartInstance.XLeft, selectedPartInstance.XTop), canvas);

                _ctlDesignBorder.SetTargetPart(selectedPartInstance);
                _ctlDesignBorder.Visibility = Visibility.Visible;

                _ctlDesignBorder.SetOffset(offsetOfPart);
                _ctlDesignBorder.SetSize(new Size(selectedPartInstance.XWidth * _ctlMainFrame.ZoomFactor,
                    selectedPartInstance.XHeight * _ctlMainFrame.ZoomFactor));
            }
            else
            {
                Point offsetOfPart = selectedPartInstance.TranslatePoint(new Point(0, 0), canvas);

                _ctlDesignBorder.SetTargetPart(selectedPartInstance);
                _ctlDesignBorder.Visibility = Visibility.Visible;

                _ctlDesignBorder.SetOffset(offsetOfPart);
                _ctlDesignBorder.SetSize(new Size(selectedPartInstance.XWidth * _ctlMainFrame.ZoomFactor,
                    selectedPartInstance.XHeight * _ctlMainFrame.ZoomFactor));
            }
        }
    }
}
